<template>
  <Modal v-model="modalView" width="360"  @on-cancel="onAgreeCancel">
    <p slot="header">
      <Icon type="ios-information-circle"></Icon>
      <span>批量设置</span>
    </p>
    <div style="text-align:center">
      <Form
        label-position="right"
        :label-width="0"
        ref="form"
        :rules="rules"
        :model="form"
      >
      <!-- <FormItem label prop="threshold" v-if="setType=='threshold'">
        <Input v-model="form.threshold" placeholder="请输入阈值"/>
      </FormItem> -->
      <FormItem label prop="integral" v-if="setType=='integral'">
        <Input v-model="form.integral" placeholder="请输入积分"/>
      </FormItem>
      <FormItem label prop="rebate" v-if="setType=='rebate'">
        <Input v-model="form.rebate" placeholder="请输入返利"/>
      </FormItem>
      <FormItem prop="sellOutQuantity" v-if="setType=='sellOutQuantity'">
        <Input v-model="form.sellOutQuantity" placeholder="请输入促销库存"/>
      </FormItem>
      <FormItem prop="sale" v-if="setType=='sale'">
        <Input v-model="form.sale" placeholder="请输入促销价"/>
      </FormItem>
      </Form>
    </div>
    <div slot="footer">
      <Button size="large" type="text" @click="onAgreeCancel">取消</Button>
      <Button type="primary" size="large" @click="onAgree">确认</Button>
    </div>
  </Modal>
</template>
<script>
export default {
  data() {
    const sellValidate = (rule, value, callback) => {
      if (!(/^[1-9]\d*$/.test(value))) {
        return callback(new Error('请输入正整数'))
      } else {
        callback()
      }
    }
    const rebateValidate = (rule, value, callback) => {
      if(!(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)|(0))$/.test(value))) {
        return callback(new Error('请输入非负数'))
      } else {
        callback()
      }
    }
    return {
      id: this.$route.query.id || '',
      modalView: false,
      setType: '',
      dataType: '',
      form: {
        threshold: "",
        integral: "",
        rebate: "",
        sellOutQuantity: '',
        sale: ''
      },
      rules: {
        threshold: [
          { required: true, message: "请输入输入阈值", trigger: "blur" },
          { validator: rebateValidate, trigger: 'blur' }
        ],
        integral: [
          { required: true, message: "请输入输入积分", trigger: "blur" },
          { validator: rebateValidate, trigger: 'blur' }
        ],
        rebate: [
          { required: true, message: "请输入输入返利", trigger: "blur" },
          { validator: rebateValidate, trigger: 'blur' }
        ],
        sellOutQuantity: [
          { required: true, message: "请输入输入促销库存", trigger: "blur" },
          { validator: sellValidate, trigger: "blur"}
        ],
        sale: [
          { required: true, message: "请输入输入促销价", trigger: "blur" },
          { validator: rebateValidate, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    reset(row)   {
      this.modalView = true
      this.setType = row.setType
      this.dataType = row.type
      this.$refs["form"].resetFields()
    },
    onAgree() {
      this.$refs["form"].validate(valid => {
        let value = ''
        let type = ''
        let text = ''
        let ids = []
        let data = []
        if (this.dataType == '1') {
          data = this.$store.state.bargain.getBgLmtData
        } else if (this.dataType == '2') {
          data = this.$store.state.bargain.getBgUnLmtData
        }
        for (let item of data) {
          for(let todo of item.productSkuList) {
            if (todo.checked) ids.push(todo.acId)
          }
        }
        let params = {
          activityId: this.id,
          ids: ids
        }
        if (!ids.length && (this.dataType == '1' || this.dataType == '2')) {
          this.$Notice.success({
            title: '温馨提示',
            desc: "请勾选相应的想要设置商品"
          })
          this.modalView = false
          return
        }
        let url = ''
        if (valid) {
          switch(this.setType) {
            case 'threshold': // 阈值
              value = this.form.threshold
              type = 'warningQuantity'
            break;
            case 'integral': // 积分
              value = this.form.integral
              type = 'giftPoint'
              url = 'setGiftPoint'
              params.giftPoint = value
              text = '积分'
            break;
            case 'rebate': // 返利
              value = this.form.rebate
              type = 'giftRebate'
              url = 'setGiftRebate'
              params.giftRebate = value
              text = "返利"
            break;
            case 'sellOutQuantity': // 促销库存
              value = this.form.sellOutQuantity
              type = 'sellOutQuantity'
              url = 'setSellOutQuantity'
              params.sellOutQuantity = value
              text = "促销库存"
            break;
            case 'sale': // 促销价
              value = this.form.sale
              type = 'sale'
              url = 'setProQuantity'
              params.promotionPrice = value
              text = '促销价'
            break; 
          }
          this.modalView = false
          if (this.dataType == '0') {
            this.$store.commit('onBargainBatchInput', {value: value, type: type, dataType: this.dataType})
          } else {
            this.$store.dispatch(url, params).then(res => {
            this.$Notice.success({
              title: '温馨提示',
              desc: res + "条" + text + "修改成功" 
            })
              this.$store.commit('onBargainBatchInput', {value: value, type: type, dataType: this.dataType})
            })
          }
        };
      })
    },
    onAgreeCancel() {
      this.modalView = false;
    }
  }
};
</script>

